<?php
//轮播图控制器
	namespace Application\Admin\Controller;
	use Application\Admin\Model\Banner as BannerModel;
	use KJ\Upload;
	class Banner extends Admin{
		public function index(){
			$bannerModel = new BannerModel();
			$this->banner = $bannerModel -> order('banner_sort asc') -> select();
			$this -> render();
		}
		public function add(){
			if(IS_POST){
				$bannerModel = new BannerModel();
				if(!empty($_FILES['show_img']['name'])){
					$upload = new Upload();
					$info = $upload -> upload('show_img');
					if(!empty($info['realPath'])){
						$bannerModel -> attributes -> banner_src = $info['realPath'];
						$bannerModel -> attributes -> banner_name = $_POST['show_name'];
						$bannerModel -> attributes -> banner_sort = $_POST['sort'];
						if($bannerModel -> save()){
							echo "<script>alert('添加成功');window.location.href='index.php?m=admin&c=banner&a=index';</script>";
						}
					}else{
						echo "<script>alert('上传信息有误');window.location.href='index.php?m=admin&c=banner&a=index';</script>";
						exit;
					}
				}else{
					echo "<script>alert('图片不能为空');window.location.href='index.php?m=admin&c=banner&a=index';</script>";
					exit;
				}
			}
		}

		public function edit(){
			if(IS_GET){
				$id = $_GET['id'];
				$bannerModel = new BannerModel();
				$this->banner = $bannerModel -> find($id);
				$this->render('edit');
			}
			if(IS_POST){
				$id = $_POST['id'];
				if(!empty($_FILES['show_img']['name'])){
					$upload = new Upload();
					$info = $upload -> upload('show_img');
					if(!empty($info['realPath'])){
						$bannerModel = new BannerModel();
						$url = $bannerModel -> field('banner_src') -> where("id = $id") -> select();
						$url = $url[0]['banner_src'];
						unlink($url);
						$bannerModel = new BannerModel();
						$banner = $bannerModel -> findModel($id);
						$banner -> attributes -> banner_src = $info['realPath'];
						$banner -> attributes -> banner_name = $_POST['show_name'];
						$banner -> attributes -> banner_sort = $_POST['sort'];

						if($banner -> save()){
							echo "<script>alert('编辑成功');window.location.href='index.php?m=admin&c=banner&a=index';</script>";
							exit;
						}
					}else{
						echo "<script>alert('上传信息有误');window.location.href='index.php?m=admin&c=banner&a=index';</script>";
						exit;
					}
				}else{
					$bannerModel = new BannerModel();
					$banner = $bannerModel -> findModel($id);
					$banner -> attributes -> banner_name = $_POST['show_name'];
					$banner -> attributes -> banner_sort = $_POST['sort'];

					if($banner -> save()){
						echo "<script>alert('编辑成功');window.location.href='index.php?m=admin&c=banner&a=index';</script>";
						exit;
					}
				}
			}	
		}
		public function delete(){
			if(IS_GET){
				$id = $_GET['id'];
				$bannerModel = new BannerModel();
				$filename = $bannerModel -> field('banner_src') ->where("id=$id")->select();
				$filename = $filename[0]['banner_src'];
				$banner = $bannerModel -> findModel($id);
				if($banner -> delete()){
					unlink($filename);		/*删除图片*/
					echo "<script>alert('删除成功');window.location.href='index.php?m=admin&c=banner&a=index';</script>";
				}
			}
		}
	}
?>